package vn.hust.homebudget;

import vn.hust.homebudget.core.broadcastreceiver.BudgetBroadcastReceiver;
import vn.hust.homebudget.core.intent.BaseManageIntent;
import vn.hust.homebudget.utily.BudgetBaseAction;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.Window;

public class BudgetBaseActivity extends Activity implements BaseManageIntent{
	private static BudgetBaseAction baseAction = new BudgetBaseAction();
	protected boolean isRunning;
	
	private BudgetBroadcastReceiver generalBroadcastReceiver;
	protected BudgetBroadcastReceiver appBroadcastReceiver;
	protected BudgetBroadcastReceiver notifyBroadcastReceiver;
	
	
	/** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS);
		requestWindowFeature(Window.FEATURE_PROGRESS);
        // catch uncaughtException from activities based on this activity 
		getBaseAction().handleUnCaughtException(this);
		// register general broadcast receiver
		generalBroadcastReceiver = new BudgetBroadcastReceiver() {			
			@Override
			public void handleReceiver(Context context, Intent intent) {
				if(getAction()!= null){
					if (compareAction(getAppIntentFilter().getAction(0))) {
						finish();
					}
				}
			}			
			@Override
			public void addActionIntentFilter() {
				getAppIntentFilter().addAction(BROADCAST_ACTION_QUIT);
				
			}
		};
		appBroadcastReceiver = new BudgetBroadcastReceiver() {			
			@Override
			public void handleReceiver(Context context, Intent intent) {}			
			@Override
			public void addActionIntentFilter() {}
		};
		
		notifyBroadcastReceiver = new BudgetBroadcastReceiver() {			
			@Override
			public void handleReceiver(Context context, Intent intent) {}			
			@Override
			public void addActionIntentFilter() {}
		};
    }
	
	/** Called when the activity comes to the foreground */
	@Override
	protected void onResume() {
		super.onResume();
		setRunning(true);
		
		registerReceiver(generalBroadcastReceiver, generalBroadcastReceiver.getAppIntentFilter());
		registerReceiver(appBroadcastReceiver, appBroadcastReceiver.getAppIntentFilter());
		registerReceiver(notifyBroadcastReceiver, notifyBroadcastReceiver.getAppIntentFilter());
		getBaseAction().removeRedundantActivity(this);
	}
	/** Called when the another activity comes in front of the activity */
	@Override
	protected void onPause() {
		super.onPause();
		setRunning(false);
	}
	/** This is called when the overall system is running low on memory, and would like actively running process to try to tighten their belt. */
	@Override
	public void onLowMemory() {
		super.onLowMemory();
		System.gc();
	}
	/** Called when the activity is shutdown */
    @Override
	protected void onDestroy() {
		super.onDestroy();
		try {unregisterReceiver(generalBroadcastReceiver);} catch (Exception ignore) {}
		try {unregisterReceiver(appBroadcastReceiver);} catch (Exception ignore) {}
		try {unregisterReceiver(notifyBroadcastReceiver);} catch (Exception ignore) {}
		System.gc();
	}
	
	@Override
	public void sendIntent(Bundle bundle, BudgetBaseActivity from, Class<?> to) {
		Intent intent = new Intent(from, to);
		intent.putExtras(bundle);
		startActivity(intent);	
	}

	@Override
	public void sendIntentBroadcast(Bundle bundle, String Action) {
		Intent intent = new Intent(Action);
		intent.putExtras(bundle);
		sendBroadcast(intent);
	}
	
	/**
	 * @return the baseAction
	 */
	public static BudgetBaseAction getBaseAction() {
		return baseAction;
	}
	/**
	 * @param baseAction the baseAction to set
	 */
	public static void setBaseAction(BudgetBaseAction baseAction) {
		BudgetBaseActivity.baseAction = baseAction;
	}
	/**
	 * @param isRunning the isRunning to set
	 */
	public void setRunning(boolean isRunning) {
		this.isRunning = isRunning;
	}
	/**
	 * @return the isRunning
	 */
	public boolean isRunning() {
		return isRunning;
	}
}
